package com.bw.coupon.controller;
/**
 * @Author：尚志豪
 * @Package：com.bw.coupon.controller
 * @Project：doudi-group-practical_training-02
 * @name：CouponController
 * @Date：2025/1/12 20:32
 */

import com.bw.common.core.domain.Result;
import com.bw.common.core.web.controller.BaseController;
import com.bw.coupon.domain.TPmsSkuInfo;
import com.bw.coupon.domain.req.TDiscountCouponReq;
import com.bw.coupon.domain.TDiscountCoupon;
import com.bw.coupon.domain.res.TDiscountCouponRes;
import com.bw.coupon.domain.vo.CouponAdd;
import com.bw.coupon.service.ICouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: doudi-group-practical_training-02
 * @description: 优惠劵控制台
 * @author: CuiFu Bo
 * @create: 2025-01-12 20:32
 **/
@RestController
@RequestMapping("/coupon")
public class CouponController extends BaseController
{
    @Autowired
    private ICouponService iCouponService;


    /**
     * B端 商家 发布优惠劵
     */
    @PostMapping("/add")
    public Result add(@RequestBody CouponAdd couponAdd)
    {
        // 判断优惠劵类型
        // 满减
        if (couponAdd.getCouponId() == 1){
            // 使用门槛大于等于满减金额
            if (couponAdd.getEmployDoorsill() >= couponAdd.getValue1())
            {
                iCouponService.add(couponAdd);
                return Result.success("商家优惠劵发布成功");
            }
            return Result.error("满减金额不能大于使用门槛");
        }
        // 随机
        if (couponAdd.getCouponId() == 3){
            // 使用门槛大于等于满减金额
            if (couponAdd.getEmployDoorsill() >= couponAdd.getValue2() && couponAdd.getEmployDoorsill() >= couponAdd.getValue1() && couponAdd.getValue1() < couponAdd.getValue2())
            {
                iCouponService.add(couponAdd);
                return Result.success("商家优惠劵发布成功");
            }
            return Result.error("随机最大金额不能大于使用门槛");
        }

        return null;
    }
    /**
     * B端 商家 多条件查询优惠卷分页列表
     */
    @PostMapping("/merchantCouponList")
    public Result merchantCouponList(@RequestBody TDiscountCouponReq tDiscountCouponReq)
    {
        startPage();
        List<TDiscountCouponRes> list = iCouponService.merchantCouponList(tDiscountCouponReq);
        return getDataTable(list);
    }
    /**
     * B端 通过优惠劵id查询优惠劵信息
     */
    @PostMapping("/accessCouponInformationByCouponId/{id}")
    public Result<TDiscountCoupon> accessCouponInformationByCouponId(@PathVariable Long id)
    {
        TDiscountCoupon tDiscountCoupon = iCouponService.accessCouponInformationByCouponId(id);
        return Result.success(tDiscountCoupon);
    }
    /**
     * B端 商家 通过优惠劵id失效
     */
    @PostMapping("/couponExpired/{id}")
    public Result couponExpired(@PathVariable Long id)
    {
        iCouponService.couponExpired(id);
        return Result.success("优惠券已失效");
    }
    /**
     * B端 商家 通过优惠劵id删除
     */
    @PostMapping("/couponDeletion/{id}")
    public Result couponDeletion(@PathVariable Long id)
    {
        iCouponService.couponDeletion(id);
        return Result.success("优惠券已删除");
    }
    /**
     * B端 发布优惠劵页面 通过商铺id查询所有商品
     */
    @PostMapping("/publishAllItemsInCoupon/{id}")
    public Result<List<TPmsSkuInfo>> publishAllItemsInCoupon(@PathVariable Long id)
    {
       List<TPmsSkuInfo> listAll = iCouponService.publishAllItemsInCoupon(id);
        return Result.success(listAll);
    }
}
